package cn.lavender.web.response.handler;

import cn.lavender.web.response.api.ApiResponse;
import cn.lavender.web.response.errorcode.CommonErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * <p>
 * 全局异常处理器
 * </p>
 *
 * @author: zhu.chen
 * @date: 2020/12/29
 * @version: v1.0.0
 */
@Configuration
@RestControllerAdvice
@Order
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理RuntimeException类及其子类
     * @param exception
     * @return
     */
    @ExceptionHandler(value = {RuntimeException.class})
    protected ApiResponse handleRuntimeException(RuntimeException exception) {
        return ApiResponse.instanceApiResponse(null, CommonErrorCodeEnum.FAIL, exception.getMessage());
    }

    /**
     * 处理Exception类及其子类
     * @param exception
     * @return
     */
    @ExceptionHandler(value = {Exception.class})
    protected ApiResponse handleException(Exception exception) {
        return ApiResponse.instanceApiResponse(null, CommonErrorCodeEnum.FAIL, exception.getMessage());
    }

    /**
     * 处理Error类及其子类
     * @param error
     * @return
     */
    @ExceptionHandler(value = {Error.class})
    protected ApiResponse handleError(Error error) {
        return ApiResponse.instanceApiResponse(null, CommonErrorCodeEnum.FAIL, error.getMessage());
    }

}